from selenium.common import NoSuchElementException
from selenium.webdriver.common.by import By
from frp_page_object.base_page import BasePage, logger
from time import sleep, time


class TocOrderMngPage(BasePage):
    loc_input_pre_order = (By.XPATH, '//*[@placeholder="来源单号"]')
    loc_input_retail_channel_orders = (By.XPATH, '//*[@placeholder="零售单号"]')
    loc_input_retail_orders = (By.XPATH, '//*[@placeholder="零售订单号"]')
    loc_input_retail_cmp = loc_input_retail_orders
    loc_search_button = (By.XPATH, '//*[contains(text(),"查 询")]')

    def click_search_button(self):
        self.click_wait(self.loc_search_button, 5)

    # 预占单是否创建成功
    def check_import_success(self, order_number, which_page):
        self.input(which_page, order_number)
        self.click_search_button()
        sleep(5)
        try:
            self.locator((By.XPATH, f'//*[contains(text(),"{order_number}")]'), 2)
        except NoSuchElementException:
            match which_page:
                case self.loc_input_pre_order:
                    logger.error(f'单号：{order_number} 未找到预占单')
                case self.loc_input_retail_channel_orders:
                    logger.error(f'单号：{order_number} 未找到零售原始订单')
                case self.loc_input_retail_orders:
                    logger.error(f'单号：{order_number} 未找到零售订单')
                case self.loc_input_retail_cmp:
                    logger.error(f'单号：{order_number} 未找到零售履约计划单')
            return False
        except Exception as e:
            logger.warning(f'check_import_success:  发生其他错误:  {e}')
            return False
        else:
            return True

    # 等待零售渠道订单处理成功
    def wait_for_process(self):
        start_time = time()
        done = False
        while time() - start_time < 120:
            try:
                self.locator((By.XPATH, '//*[contains(text(),"成功")]'), 3)
            except NoSuchElementException:
                try:
                    self.locator((By.XPATH, '//div[@class="ag-center-cols-viewport"]//*[contains(text(),"失败")]'))
                except NoSuchElementException:
                    self.click_search_button()
                else:
                    logger.error('零售原始订单处理失败')
                    return
            else:
                done = True
                break
        if not done:
            logger.error('零售渠道单两分钟内未处理成功')

    # 等待履约计划生成
    def wait_for_generation(self):
        start_time = time()
        done = False
        while time() - start_time < 120:
            try:
                self.locator((By.XPATH, '//*[contains(text(),"已生成")]'), 3)
            except NoSuchElementException:
                try:
                    self.locator((By.XPATH, '//*[contains(text(),"生成失败")]'))
                except NoSuchElementException:
                    self.click_search_button()
                else:
                    logger.error('履约计划生成失败')
                    return
            else:
                done = True
                break
        if not done:
            logger.error('履约计划两分钟内未成功生成')

    def check_distribution(self):
        try:
            self.locator((By.XPATH, f'//*[contains(text(),"已分货")]'))
        except NoSuchElementException:
            logger.error('未分货！')
            return False
        else:
            return True

    def check_delivery(self):
        self.click((By.XPATH, '//*[@class="ag-pinned-left-header "]//input'))
        self.click((By.XPATH, '//button//*[contains(text(),"确认送货")]'))
        self.click((By.XPATH, '//*[contains(text(),"确 定")]'))
        sleep(2)
        self.click_search_button()
        try:
            self.locator((By.XPATH, f'//*[contains(text(),"已锁定")]'), 3)
            self.locator((By.XPATH, f'//*[contains(text(),"已确认")]'))
        except NoSuchElementException:
            logger.warning('订单确认送货后未锁定或显示未确认！')
            return False
        else:
            return True

    def check_notices(self):
        self.click((By.XPATH, '//*[@class="ag-pinned-left-header "]//input'))
        self.click((By.XPATH, '//button//*[contains(text(),"配送通知")]'))
        self.click((By.XPATH, '//*[contains(text(),"确 定")]'))
        sleep(2)
        self.click_search_button()
        try:
            sleep(1)
            self.locator((By.XPATH, f'//*[contains(text(),"已通知")]'), 5)
        except NoSuchElementException:
            logger.warning('发送配送通知后显示未通知')
            return False
        else:
            return True
